/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.tutwiz.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.tutwiz.model.Vote;
import net.tutwiz.persistence.VotePersistenceManager;

/**
 *
 * @author viktor
 */
public class VoteController extends Controller {

    private VotePersistenceManager pm = new VotePersistenceManager();

    public VoteController(String[] params, HttpServletRequest request, HttpServletResponse response) {
        super(params, request, response);
    }

	// Anropar metoden för den action som ska köras
    @Override
    public void doAction() {
        if ("".equals(this.action)) {
            // Default action
            this.all();
        } else if ("all".equals(this.action)) {
            this.all();
        } else if ("show".equals(this.action)) {
            this.show();
        }
    }

    private void all() {

        List<Vote> votes = pm.getAll();
        this.render(votes);

    }

    private void show() {


        if (parameters.length > 0) {
            int id;
            try {
                id = Integer.parseInt(parameters[0]);
            } catch (Exception ex) {
                id = -1;
            }
            if (id > 0) {
                Vote vote = pm.get(Long.valueOf(id));
                this.render(vote);
            } else {
                this.niceDie("Vote ID must be an integer!", 400);
            }
        } else {
            this.niceDie("Vote ID missing!", 400);
        }
    }
}
